package com.project.weimi.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AgeUtil {

	public static int getAge(Date birthDay) throws Exception {

		// 获取当前系统时间
		Calendar cal = Calendar.getInstance();
		// 如果出生日期大于当前时间，则抛出异常
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException("生日超过当前时间，无效的出生日期！");
		}

		// 取出系统当前时间的年、月、日部分
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		// 将日期设置为出生日期
		cal.setTime(birthDay);
		// 取出出生日期的年、月、日部分
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		// 当前年份与出生年份相减，初步计算年龄
		int age = yearNow - yearBirth;
		if(age <= 0){
			age = 0;
			return 0;
		}
		// 当前月份与出生日期的月份相比，如果月份小于出生月份，则年龄上减1，表示不满多少周岁
		if (monthNow <= monthBirth) {
			// 如果月份相等，在比较日期。如果当前日，小于出生日，也减1，表示不满多少周岁
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;
				
			} else {
				age--;
			}
			
			if(age <= 0){
				age = 0;
			}
		}
		
		return age;
	}

	// 第二种方法（备用）
	@SuppressLint("SimpleDateFormat")
	public static int getAge2(Date birthDate) {
		if (birthDate == null)
			throw new RuntimeException("出生日期不能为null");
		int age = 0;
		Date now = new Date();
		SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
		SimpleDateFormat format_M = new SimpleDateFormat("MM");
		String birth_year = format_y.format(birthDate);
		String this_year = format_y.format(now);
		String birth_month = format_M.format(birthDate);
		String this_month = format_M.format(now);
		// 初步，估算
		age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);
		// 如果未到出生月份，则age - 1
		if (this_month.compareTo(birth_month) < 0)
			age -= 1;
		if (age < 0)
			age = 0;
		return age;
	}
	
	/**
	 * 通过日期来确定星座
	 * 
	 * @param month
	 * @param day
	 * @return
	 */
	public static String getStarSeat(int month, int day) {
		String starSeat = null;

		if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
			starSeat = "白羊座";
		} else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
			starSeat = "金牛座";
		} else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
			starSeat = "双子座";
		} else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
			starSeat = "巨蟹座";
		} else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
			starSeat = "狮子座";
		} else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
			starSeat = "处女座";
		} else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
			starSeat = "天秤座";
		} else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
			starSeat = "天蝎座";
		} else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
			starSeat = "射手座";
		} else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
			starSeat = "摩羯座";
		} else if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
			starSeat = "水瓶座";
		} else {
			starSeat = "双鱼座";
		}
		return starSeat;
	}
	
	// 把日期转为字符串
	@SuppressLint("SimpleDateFormat")
	public static String converToString(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(date);
	}

	// 把字符串转为日期
	@SuppressLint("SimpleDateFormat")
	public static Date converToDate(String strDate) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.parse(strDate);
	}

	
}
